package com.nttuan.service2;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.nttuan.model.dao.TaskDao;
import com.nttuan.model.dao.TrackDao;
import com.nttuan.model.dao.service.TaskService;
import com.nttuan.model.dao.service.TrackService;
import com.nttuan.model.entity.Alarm;
import com.nttuan.model.entity.Track;
import com.nttuan.util.LogSupport;
import com.nttuan.util.Support;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by TuanNT on 11/26/2014.
 */
public class AlarmService extends Service {
    private final int DELAY = 5000;
    private final String TAG = "alarmService";
    public static String ACTION = "action";

    private TaskService taskService;
    private RefreshAlarm refreshAlarm;
    private Timer timer;

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind");
        return null;
    }

    @Override
    public void onCreate() {
        taskService = TaskDao.getInstance(AlarmService.this);
        timer = new Timer();
        refreshAlarm = new RefreshAlarm();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        timer.scheduleAtFixedRate(refreshAlarm, 0, DELAY);
        Log.d(TAG, "started");
        return START_STICKY;
    }

    public void stop(){
        refreshAlarm.cancel();
        timer.cancel();
        stopSelf();
        Log.d(TAG,"stop service");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stop();
    }

    class RefreshAlarm extends TimerTask {
        private final String TAG = "RefreshAlarm";
        private Date now, previous = Support.removeMilliSecond(Calendar.getInstance().getTime());
        @Override
        public void run() {

            now = Support.removeMilliSecond(Calendar.getInstance().getTime());
            if (now.getTime() - nextMinute(previous).getTime() == 0) {
                if (!LogSupport.lastUpdate(AlarmService.this)) {
                    LogSupport.saveLog(AlarmService.this, taskService.getAlarm(now));
                }
                previous = now;
                List<Alarm> alarms = LogSupport.getLogAlarm(AlarmService.this, now, taskService);
                saveTrack(alarms);
            }
        }

        private void saveTrack(List<Alarm> alarms) {
            TrackService trackService = TrackDao.getInstance(AlarmService.this);
            Date now = Calendar.getInstance().getTime();
            Track track;
            for (Alarm alarm : alarms) {
                track = new Track();
                if (alarm.isStated())
                    track.setActon(Track.ACTION_EVENT_START);
                else
                    track.setActon(Track.ACTION_ALARM);
                track.setLastTime(now);
                track.setTask(alarm.getTask());
                track.setActive(true);
                trackService.create(track);
            }
        }
        private Date nextMinute(Date currentDate) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.MINUTE, 1);
            return calendar.getTime();

        }
    }

}
